<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Hash;
use App\Http\Requests\AdminloginRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class ListController extends Controller
{
   /*
	前台商品列表页
   */
    public function getIndex($id)
    {      
    	
     	// echo '前台商品列表页';
    	// // 查询数据
    	// $goods = DB::table('goods')->get();
    	// // dd($goods);
    	// return view('home.list.index',['goods'=>$goods,'cid'=>$id]);
        $res = [];
        if($id < 7){
            $arr = DB::table('cate')->where('pid',$id)->select('id')->get();
            foreach ($arr as $k => $v) {
                $res[] = $v->id;
            }
            // dd($res);
            $goods = DB::table('goods')->whereIn('cid',$res)->paginate(6);
            // dd($goods);
        }else{
            $goods = DB::table('goods')->where('cid',$id)->paginate(6);
        }
        return view('home.list.index',['goods'=>$goods]);
    }
    /*
	前台商品详情页
    */
    public function getDetails($id)
    {
    	//查询数据
        // echo '前台商品列表页';
        // die;
    	$goods = DB::table('goods')->where('id',$id)->first();

        //根据$goods查订单号 查评论
        $comment = DB::table('comment')
        ->join('user','comment.uid','=','user.id')
        ->select('comment.*','user.username')
        ->where('dingdan_id',$goods->dingdan_id)
        ->get();
        //查询该用户是否收藏了该物品
        $res1 = DB::table('collect')->where('uid',session('id'))->where('goods_id',$id)->get();
        // dd($res);
        if($res1){
            //已收藏
            $res = 1;
        }else{
            //未收藏
            $res = 0;
        }
        // dd($res);
    	$size = explode(',',$goods->size);
    	$color = explode(',',$goods->color);
    	// dd($size);
    	// echo "这是详情页面";
    	return view('home.list.details',['goods'=>$goods,'size'=>$size,'color'=>$color,'res'=>$res,'comment'=>$comment]);
    }
  
}
